package org.example.java8.stream;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class CreateStream {
    public static void main(String[] args) {
        //CreateStreamFromCollection().forEach(System.out::println);
        //CreateStreamFromValues().forEach(System.out::println);
        //CreateStreamFromArrays().forEach(System.out::println);

        //Stream<String> streamFromFile = createStreamFromFile();
        //System.out.println(streamFromFile);

        //CreateStreamFromIterate().forEach(System.out::println);
        //CreateStreamFromGenerate().forEach(System.out::println);
        CreateObjStreamFromGenerate().forEach(System.out::println);
    }

    //Collection提供了stream()方法，通过Collections创建一个stream，元素的顺序是保持一致的
    private static Stream<String> CreateStreamFromCollection() {
        List<String> list = Arrays.asList("hello", "world", "java", "c++");
        return list.stream();
    }

    //Stream有个静态方法of()，可以由values得到一个stream
    private static Stream<String> CreateStreamFromValues() {
        return Stream.of("hello", "world", "java", "c++");
    }

    //通过Arrays得到一个stream
    private static Stream<String> CreateStreamFromArrays() {
        String[] strings = {"hello", "world", "java", "c++"};
        return Arrays.stream(strings);
    }

    //通过文件得到一个stream
    private static Stream<String> createStreamFromFile() {
        Path path = Paths.get("D:\\IdeaProjects\\java8\\java8-sharing\\src\\main\\java\\org\\example\\java8\\stream\\Dish.java");
        try (Stream<String> streamFromFile = Files.lines(path)) {
            streamFromFile.forEach(System.out::println);
            return streamFromFile;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //通过iterate得到一个stream
    private static Stream<Integer> CreateStreamFromIterate() {
        //iterate()返回一个无限的stream
        Stream<Integer> stream = Stream.iterate(0, n -> n + 2).limit(10);
        return stream;
    }

    //通过generate得到一个stream
    private static Stream<Double> CreateStreamFromGenerate() {
        //generate()返回一个无限的stream
        Stream<Double> stream = Stream.generate(Math::random).limit(10);
        return stream;
    }

    //通过generate得到一个stream
    private static Stream<Obj> CreateObjStreamFromGenerate() {
        //generate()返回一个无限的stream
        Stream<Obj> stream = Stream.generate(new ObjSupplier()).limit(10);
        return stream;
    }

    static class ObjSupplier implements Supplier<Obj> {
        private int index = 0;
        private Random random = new Random(System.currentTimeMillis());

        @Override
        public Obj get() {
            index = random.nextInt(100);
            return new Obj(index, "Name->" + index);
        }
    }

    static class Obj {
        private int id;
        private String name;

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public Obj(int id, String name) {
            this.id = id;
            this.name = name;
        }

        @Override
        public String toString() {
            return "Obj{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
}
